var data1 = [{'id':'1','foreignbankcharges':'text1','domesticbanksdeductfees':'text2','refundamount':'text3','contractunit':'text4','cdate':'text5','ddate':'text6','placeofdelivery':'text7'},{'id':'2','foreignbankcharges':'text1','domesticbanksdeductfees':'text2','refundamount':'text3','contractunit':'text4','cdate':'text5','ddate':'text6','placeofdelivery':'text7'},
    {'id':'3','foreignbankcharges':'text1','domesticbanksdeductfees':'text2','refundamount':'text3','contractunit':'text4','cdate':'text5','ddate':'text6','placeofdelivery':'text7'},{'id':'4','foreignbankcharges':'text1','domesticbanksdeductfees':'text2','refundamount':'text3','contractunit':'text4','cdate':'text5','ddate':'text6','placeofdelivery':'text7'}];

var data2 = [{'id':'1','foreignbankcharges':'text1','domesticbanksdeductfees':'text2','refundamount':'text3','contractunit':'text4','cdate':'text5','ddate':'text6','placeofdelivery':'text7'},{'id':'2','foreignbankcharges':'text1','domesticbanksdeductfees':'text2','refundamount':'text3','contractunit':'text4','cdate':'text5','ddate':'text6','placeofdelivery':'text7'},
    {'id':'3','foreignbankcharges':'text1','domesticbanksdeductfees':'text2','refundamount':'text3','contractunit':'text4','cdate':'text5','ddate':'text6','placeofdelivery':'text7'},{'id':'4','foreignbankcharges':'text1','domesticbanksdeductfees':'text2','refundamount':'text3','contractunit':'text4','cdate':'text5','ddate':'text6','placeofdelivery':'text7'}];

var layer = layui.layer //弹层
    ,table = layui.table //表格
    form = layui.form,$ = layui.jquery,laydate = layui.laydate,cbLists=[], id = getQueryVariable("id");

if(id == ""){
    //获取表格选择的数据
    var cbList = parent.table.checkStatus("maininformationtableid").data;
//如果没有选择，则默认第一条
    if(cbList.length == 0){
        cbLists.push(parent.table.cache["maininformationtableid"][0]);
    }else{
        for(var i = 0;i<cbList.length;i++){
            cbLists.push(cbList[i]);
        }
    }
}else{
    //点击行收款拆分进入 ajax根据ID查询收款单据信息，这是前端测试数据
    cbLists.push({id:id,paymentorderno:"付款编号",paymentdate:"付款日期"})
}

table.render({
    elem: '#mains'
    ,data:cbLists
    ,cols: [[
        {field:'paymentorderno', title:'付款编号',sort:true,width: 120}
        ,{field:'paymentdate', title:'付款日期',sort:true,width: 120}
        ,{field:'suppliername', title:'供应商名称',sort:true,width: 120}
        ,{field:'currency', title:'币种',sort:true,width: 120}
        ,{field:'mjexchangerate', title:'美金汇率',sort:true,width: 120}
        ,{field:'payableamount', title:'应付金额',sort:true,width: 120}
        ,{field:'applyamount', title:'申请金额',sort:true,width: 120}
        ,{field:'realpayamount', title:'实付金额',sort:true,width: 120}
        ,{field:'paymenttype', title:'款项类型',sort:true,width: 120}
        ,{field:'splitamount', title:'待拆分金额',sort:true,width: 120}
        ,{field:'tobreakupamount', title:'未拆分金额',sort:true,width: 120}
        ,{field:'undertakepersonnel', title:'承担人员',sort:true,width: 120}
        ,{field:'theoperator', title:'操作员',sort:true,width: 120}
    ]]
    ,id: 'mainstableid'
    ,height: 300
    ,page: true
    ,done: function (res, curr, count) {
        table.render({
            elem: '#childinformation'
            ,data:data2
            ,cols: [[
                {field: 'invoicenumber',  title: '发票编号',sort:true},
                {field: 'purchaseorderno',  title: '采购单号',sort:true},
                {field: 'exportcontractno',  title: '外销合同号',sort:true},
                {field: 'refundamount',  title: '拆分金额 ',edit:'text'},
                {field: 'totalamountofpurchasecontract',  title: '采购合同总额',sort:true},
                {field: 'amountinvoice',  title: '发票总额',sort:true},
                {field:'note', title:'备注',edit:'text'},
                 {fixed: 'right', align: 'center', toolbar: '#childinformationeditor', title: '操作'}
            ]]
            ,id: 'childinformationtableid'
            ,where: {
                id:res.data.length == 0 ? "":res.data[0].id //默认第一条信息，数据为空则id为空
            }
            ,page: true
            ,height: 300
        });
    }
});

//单击行事件
table.on('row(mainstable)', function (obj) {
    table.reload('childinformationtableid', {
        data:data1
        ,where: obj.data.id //点击行ID
    }, 'data');
});

table.on('tool(childinformationtable)', function (obj) {
    if (obj.event === 'del') {
        layer.confirm('真的删除行么', function (index) {
            //需要删除的ID：data.id
            obj.del();
            layer.msg("删除成功", {icon: 6});
            layer.close(index);
        });
    }
});

table.on('edit(childinformationtable)',function (obj) {
    var old=$(this).prev().text();//旧值
    //判断数据类型 ,,,,,,,
    if((obj.field== "refundamount")&&!isNumber(obj.value)) {
        layer.msg('请输入数字');
        $(this).val(old);
        return;
    }
});

$("#akeybreakup").on("click",function () {
    //执行接口
    layer.msg("一键拆分成功", {icon: 6});
    setTimeout(function () {
        //保存成功则关闭此页面
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    },1000)
})

$("#choose").on("click",function () {
    //执行接口
    layer.msg("保存成功", {icon: 6});
    setTimeout(function () {
        //保存成功则关闭此页面
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    },1000)
})
$("#shutdown").on("click",function () {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
});

function isNumber(val) {
    var regPos = /^\d+(\.\d+)?$/; //非负浮点数
    var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
    if (regPos.test(val) || regNeg.test(val)) {
        return true;
    } else {
        return false;
    }
}